<?php
namespace common\services\position;

use linslin\yii2\curl\Curl;
use Yii;
/**
 * Created by PhpStorm.
 * User: eycuit
 * Date: 2020/2/27
 * Time: 15:14
 */

class PositionAliyun {

    public $secretCode;
    public $secretKey;

    public function __construct()
    {
        $this->secretCode = Yii::$app->params['ALIYUN_MARKET_APP_CODE'];
        $this->secretKey = Yii::$app->params['ALIYUN_MARKET_APP_SECRET'];
    }

    /**
     * 手机号归属地
     * 参考地址 https://market.aliyun.com/products/57126001/cmapi022206.html?spm=5176.730005.result.2.25cc3524h7ZLS1&innerSource=search_%E6%89%8B%E6%9C%BA%E5%8F%B7%E5%BD%92%E5%B1%9E%E5%9C%B0#sku=yuncode1620600000
     * @param string $phone
     *
     * @return mixed
     */
    public function mobilePosition($phone = '') {

        $apiUrl = 'https://api04.aliyun.venuscn.com/mobile';


        $curl = new Curl();

        $curl->setHeader('Authorization', "APPCODE " . $this->secretCode);
        $params['mobile'] = $phone;
        $curl->setGetParams($params);
        $res = $curl->get($apiUrl);
        $res = json_decode($res, true);
        if ($res['ret'] == 200) {
            return $res['data'];
        } else {
            return null;
        }
    }

}